# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
#
# secfixes:
#   2.6.6-r0:
#     - CVE-2020-10663
#     - CVE-2020-10933
#   2.6.5-r0:
#     - CVE-2019-16255
#     - CVE-2019-16254
#     - CVE-2019-15845
#     - CVE-2019-16201
#   2.5.2-r0:
#     - CVE-2018-16395
#     - CVE-2018-16396
#   2.5.1-r0:
#     - CVE-2017-17742
#     - CVE-2018-6914
#     - CVE-2018-8777
#     - CVE-2018-8778
#     - CVE-2018-8779
#     - CVE-2018-8780
#   2.4.2-r0:
#     - CVE-2017-0898
#     - CVE-2017-10784
#     - CVE-2017-14033
#     - CVE-2017-14064
#     - CVE-2017-0899
#     - CVE-2017-0900
#     - CVE-2017-0901
#     - CVE-2017-0902
#   2.4.3-r0:
#     - CVE-2017-17405
#
pkgname=ruby
pkgver=2.7.1
_abiver="${pkgver%.*}.0"
pkgrel=3
pkgdesc="An object-oriented language for quick and easy programming"
url="https://www.ruby-lang.org/"
arch="all"
license="Ruby BSD-2-Clause"
depends="ca-certificates"
depends_dev="$pkgname=$pkgver-r$pkgrel gmp-dev"
makedepends="$depends_dev zlib-dev openssl-dev gdbm-dev db-dev readline-dev
	libffi-dev coreutils yaml-dev linux-headers autoconf"
install="$pkgname.post-upgrade"
subpackages="$pkgname-dbg $pkgname-doc $pkgname-dev
	$pkgname-bigdecimal
	$pkgname-dbm
	$pkgname-etc
	$pkgname-fiddle
	$pkgname-gdbm
	$pkgname-io-console:io_console
	$pkgname-irb::noarch
	$pkgname-json
	$pkgname-minitest::noarch
	$pkgname-net-telnet:net_telnet:noarch
	$pkgname-power_assert::noarch
	$pkgname-rake::noarch
	$pkgname-rdoc::noarch
	$pkgname-sdbm
	$pkgname-test-unit:test_unit:noarch
	$pkgname-webrick::noarch
	$pkgname-xmlrpc::noarch
	$pkgname-libs
	$pkgname-full::noarch
	"
source="https://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/ruby-$pkgver.tar.gz
	rubygems-avoid-platform-specific-gems.patch
	test_insns-lower-recursion-depth.patch
	fix-get_main_stack.patch
	openssl-config-support-include-directive.patch
	arm-coroutines.patch
	"
replaces="ruby-gems"

_gemdir="/usr/lib/ruby/gems/$_abiver"
_rubydir="/usr/lib/ruby/$_abiver"
_chost="${CHOST/-alpine-/-}"

case "$CARCH" in
	x86) _arch="i386";;
	*) _arch="$CARCH";;
esac

# s390x/mips require libucontext
case "$CARCH" in
	s390x|mips*)
		depends_dev="$depends_dev libucontext-dev"
		makedepends="$makedepends libucontext-dev"
		;;
esac

_bundled_gems=
for _i in $subpackages; do
	case "$_i" in
		*-dev | *-doc | *-libs | *-full:*) continue;;
		*) _bundled_gems="$_bundled_gems ${_i%%:*}";;
	esac
done

prepare() {
	default_prepare

	# XXX: Fix broken symlinks pointing to nowhere.
	rm tool/config.sub tool/config.guess
	touch tool/config.sub tool/config.guess
	update_config_sub
	update_config_guess

	autoconf
}

build() {
	# -fomit-frame-pointer makes ruby segfault, see gentoo bug #150413
	# In many places aliasing rules are broken; play it safe
	# as it's risky with newer compilers to leave it as it is.
	export CFLAGS="$CFLAGS -fno-omit-frame-pointer -fno-strict-aliasing"
	export CPPFLAGS="$CPPFLAGS -fno-omit-frame-pointer -fno-strict-aliasing"

	# ruby saves path to install. we want use $PATH
	export INSTALL=install

	# the configure script does not detect isnan/isinf as macros
	export ac_cv_func_isnan=yes
	export ac_cv_func_isinf=yes
	case "$CARCH" in
		s390x|mips*)
			export LIBS="-lucontext"
			;;
	esac

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-sitedir='/usr/local/lib/site_ruby' \
		--with-search-path="/usr/lib/site_ruby/\$(ruby_ver)/$_arch-linux" \
		--enable-pthread \
		--disable-rpath \
		--enable-shared \
		--with-mantype=man
	make
}

check() {
	make test
}

package() {
	make DESTDIR="$pkgdir" install

	install -m 644 -D COPYING "$pkgdir"/usr/share/licenses/$pkgname/COPYING

	# Remove bundled gem bundler; it's provided by a separate aport/package
	# ruby-bundler.
	subpkgdir=".trash" _mvgem bundler && rm -Rf .trash
	rm usr/bin/bundle usr/bin/bundler

	# Remove bundled CA certificates; they are provided by ca-certificates.
	rm "$pkgdir"/$_rubydir/rubygems/ssl_certs/*/*.pem
	rmdir "$pkgdir"/$_rubydir/rubygems/ssl_certs/* || true

	rm -Rf "$pkgdir"/$_gemdir/cache/*

	if [ -d "$pkgdir"/usr/local ]; then
		local f=$(cd "$pkgdir" ; find usr/local -type f)
		if [ -n "$f" ]; then
			error "Found files in /usr/local:"
			echo "$f"
			return 1
		fi
		rm -r "$pkgdir"/usr/local
	fi
}

bigdecimal() {
	pkgdesc="Ruby arbitrary-precision floating point decimal arithmetic"
	license="Ruby"
	depends="$pkgname-libs"

	_mvgem bigdecimal
}

dbm() {
	pkgdesc="A Ruby wrapper for the UNIX-style Database Manager Library"
	license="BSD-2-Clause"
	depends=""

	_mvgem dbm
}

etc() {
	pkgdesc="Provides access to information typically stored in UNIX /etc directory"
	license="BSD-2-Clause"
	depends=""

	_mvgem etc
}

fiddle() {
	pkgdesc="A libffi wrapper for Ruby"
	license="BSD-2-Clause"
	depends=""

	_mvgem fiddle
}

gdbm() {
	pkgdesc="Ruby extension for GNU dbm"
	license="BSD-2-Clause"
	depends=""

	_mvgem gdbm
}

io_console() {
	pkgdesc="Ruby simple console IO library"
	license="BSD-2-Clause"
	depends=""

	_mvgem io-console
	_mv $_rubydir/io \
		$_rubydir/$_chost/io/console.so
}

irb() {
	pkgdesc="The Interactive Ruby"
	replaces="$pkgname"
	depends="$pkgname"

	cd "$pkgdir"
	_mvgem irb
	_mv usr/bin/irb
}

json() {
	pkgdesc="JSON implementation as a Ruby extension in C"
	license="Ruby"
	depends=""

	_mvgem json
}

minitest() {
	pkgdesc="Ruby suite of testing facilities"
	license="MIT"
	depends="$pkgname"

	_mvgem minitest
}

net_telnet() {
	pkgdesc="Telnet client functionality for Ruby"
	depends="$pkgname-libs"

	_mvgem net-telnet
}

power_assert() {
	pkgdesc="Power Assert for Ruby"
	depends="$pkgname-libs"

	_mvgem power_assert
}

rake() {
	pkgdesc="Ruby based make-like utility"
	license="MIT"
	depends="$pkgname"

	_mvgem rake
	_mv usr/bin/rake
}

rdoc() {
	pkgdesc="Ruby documentation tool"
	license="Ruby"
	depends="$pkgname $pkgname-json $pkgname-io-console"

	_mvgem rdoc
	_mv usr/bin/ri \
		usr/bin/rdoc
}

sdbm() {
	pkgdesc="A simple file-based key-value store with String keys and values for Ruby"
	license="BSD-2-Clause"
	depends=""

	_mvgem sdbm
}

test_unit() {
	pkgdesc="An xUnit family unit testing framework for Ruby"
	license="Ruby PSFL"
	depends="$pkgname $pkgname-power_assert"

	_mvgem test-unit
}

webrick() {
	pkgdesc="HTTP server toolkit for Ruby"
	license="BSD-2-Clause"
	depends="$pkgname"

	_mvgem webrick
}

xmlrpc() {
	pkgdesc="XMLRPC library for Ruby"
	license="Ruby"
	depends="$pkgname-libs"

	_mvgem xmlrpc
}

libs() {
	pkgdesc="Libraries necessary to run Ruby"
	depends=""

	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
}

full() {
	pkgdesc="Ruby with all bundled gems"
	# bundler is bundled since Ruby 2.6, so include it in ruby-full despite
	# that it's provided by a seprate aport/package.
	depends="ruby $_bundled_gems ruby-bundler"

	mkdir -p "$subpkgdir"
}

_mv() {
	local i; for i in "$@"; do
		mkdir -p "$subpkgdir"/${i%/*}
		mv "$pkgdir"/$i "$subpkgdir"/${i%/*}/
	done
}

_mvgem() {
	cd "$pkgdir"
	local path; for path in ./$_rubydir/$1.rb \
			./$_rubydir/$1 \
			./$_rubydir/$_chost/$1 \
			./$_rubydir/$_chost/$1.so \
			./$_gemdir/gems/$1-* \
			./$_gemdir/specifications/$1-* \
			./$_gemdir/specifications/default/$1-* ; do
		[ ! -e "$pkgdir/$path" ] || _mv "$path"
	done
}

sha512sums="d54ec78d46644269a200cc64c84beed1baaea74189e0ffc167f90f4b9540bb6d9e7b19807c0990e1b13738b83d1e2bb4c712396d033db6a7501e6046fff12839  ruby-2.7.1.tar.gz
cfdc5ea3b2e2ea69c51f38e8e2180cb1dc27008ca55cc6301f142ebafdbab31c3379b3b6bba9ff543153876dd98ed2ad194df3255b7ea77a62e931c935f80538  rubygems-avoid-platform-specific-gems.patch
814fe6359505b70d8ff680adf22f20a74b4dbd3fecc9a63a6c2456ee9824257815929917b6df5394ed069a6869511b8c6dce5b95b4acbbb7867c1f3a975a0150  test_insns-lower-recursion-depth.patch
8d730f02f76e53799f1c220eb23e3d2305940bb31216a7ab1e42d3256149c0721c7d173cdbfe505023b1af2f5cb3faa233dcc1b5d560fa8f980c17c2d29a9d81  fix-get_main_stack.patch
a67813d7aa3553ed336f04b17461c5129546afb71a2a7cca6d1b1c860f8dd5839ca2f7695c971369f295aced3580687a28881ccd6c305f6dbdfe6b0ecf584d0e  openssl-config-support-include-directive.patch
eaee5cd1b11506df5d28d6ac909b0eae55d88e7fbb471a0cee1be7293934980a36616603a5bcb5cf8bf8518e5f313e2bba566c52bd57afe62505c8e02b0a7b87  arm-coroutines.patch"
